package top.honlnk.czuan.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.honlnk.czuan.common.config.ConstantConfig;
import top.honlnk.czuan.common.constant.AutoLoginOptionConstant;
import top.honlnk.czuan.common.result.PageResult;
import top.honlnk.czuan.common.result.Result;
import top.honlnk.czuan.common.utils.BitmaskUtil;
import top.honlnk.czuan.pojo.dto.AddUserDTO;
import top.honlnk.czuan.pojo.dto.LoginDTO;
import top.honlnk.czuan.pojo.dto.PageGetUserDTO;
import top.honlnk.czuan.pojo.entity.User;
import top.honlnk.czuan.pojo.vo.LoginVO;
import top.honlnk.czuan.service.UserService;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private BitmaskUtil bitmaskUtil;

    @Autowired
    private ConstantConfig constantConfig;

    @PutMapping("/add")
    public Result<String> addUser(@RequestBody AddUserDTO addUserDTO) {
        if (addUserDTO.getAccount().isEmpty()) {
            throw new RuntimeException("账号不能为空");
        }
        if (addUserDTO.getUname().isEmpty()) {
            throw new RuntimeException("用户名不能为空");
        }
        if (addUserDTO.getPassword().isEmpty()) {
            throw new RuntimeException("密码不能为空");
        }
        // 判断权限列表是否合法
        bitmaskUtil.verifyPermission(addUserDTO.getPowerList());

        userService.addUser(addUserDTO);
        return Result.success();
    }

    @GetMapping("/getUserByAccount")
    public Result<User> getUserByAccount(String account) {
        User user = userService.getUserByAccount(account);

        return Result.success(user);
    }

    @GetMapping("/getUser")
    public Result<PageResult> getUser(PageGetUserDTO pageGetUserDTO) {
        PageResult pageResult = userService.getUser(pageGetUserDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录，参数为: {}", loginDTO);
        if (loginDTO.getAccount() == null || loginDTO.getAccount().isEmpty()) {
            throw new RuntimeException("账号不能为空");
        }
        if (loginDTO.getPassword() == null || loginDTO.getPassword().isEmpty()) {
            throw new RuntimeException("密码不能为空");
        }
        log.info("自动登录选项为: {}", loginDTO.getAutoLoginOption());
        if (!AutoLoginOptionConstant.OPTIONS.contains(loginDTO.getAutoLoginOption())) {
            throw new RuntimeException("自动登录选项无效");
        }
        LoginVO loginVO = userService.login(loginDTO);
        return Result.success(loginVO);
    }

    @PutMapping("/updatePower")
    public Result<String> updatePower(@RequestBody User user) {
        if (user.getId() == null) {
            throw new RuntimeException("用户id不能为空");
        }
        // 不能修改root管理员的信息 // TODO 另外还不能修改同样由管理用户权限的用户的信息，后期再说，暂时不做。
        if (user.getId().equals(constantConfig.getROOT_USER_ID())) {
            throw new RuntimeException("您无权修改此用户信息");
        }
        return Result.success();
    }
}
